from pymysql import Connection
import json
conn = Connection(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    password = '123456'
)
conn.select_db("py_sql")
cursor = conn.cursor()
sql = "SELECT * FROM orders"
cursor.execute(sql)
res : tuple = cursor.fetchall()
f = open("./MYSQL案例2.txt","w",encoding="UTF-8")
f_write = ''
for x in res:
    x_dict = {"date":f"{x[0]}","order_id":f"{x[1]}","money":f"{x[2]}","province":f"{x[3]}"}
    x_json = json.dumps(x_dict)
    print(x_json)
    f_write = f_write + "\n" + x_json
f_write = f_write.strip()
f.write(f_write)
f.flush()
f.close()
conn.close()